package com.zzyweb.rentalcarsys.controller;

import cn.hutool.core.util.IdUtil;
import com.zzyweb.rentalcarsys.entity.viewobject.UserVO;
import com.zzyweb.rentalcarsys.service.UserService;
import com.zzyweb.rentalcarsys.utils.PageResult;
import com.zzyweb.rentalcarsys.utils.ResponseObject;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author zhiyi
 * @date 10/5/25
 **/
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("/all")
    ResponseObject<PageResult> getAll(@RequestParam Long currentPage, @RequestParam Long pageSize, HttpServletRequest request) {
        System.out.println(request.getRequestURI());
        PageResult<UserVO> pageResult = userService.getUsers(currentPage, pageSize, null);
        pageResult.getRecords().stream().forEach(System.out::println);
        return ResponseObject.ok(pageResult);
    }
    @PostMapping("/conditions")
    ResponseObject getByConditions(@RequestParam Long currentPage, @RequestParam Long pageSize, @RequestBody UserVO userVO) {
        PageResult<UserVO> pageResult = userService.getUsers(currentPage,pageSize,userVO);
        pageResult.getRecords().stream().forEach(System.out::println);
        return ResponseObject.ok(pageResult);
    }
    @DeleteMapping("/deleteOne/{id}")
    ResponseObject deleteOne(@PathVariable String id) {
        userService.deleteUser(id);
        return ResponseObject.ok();
    }
    @DeleteMapping("/deleteBatch/{ids}")
    ResponseObject deleteBatch(@PathVariable String ids) {
        userService.deleteUserBatch(ids);
        return ResponseObject.ok();
    }

    @PostMapping("/add")
    ResponseObject addUser(@RequestBody UserVO userVO) {
        userService.addUser(userVO);
        return ResponseObject.ok();
    }

    @PostMapping("/uploadAvatar")
    ResponseObject fileUpload(@RequestBody MultipartFile file) throws IOException {
        //设定存储路径
        String path = "/Users/zhiyi/woniu/nginxdata";
        //设定访问路径
        String baseUrl = "http://localhost:8080/";
        //设置文件前缀
        String prefix = IdUtil.getSnowflakeNextIdStr();
        //获得原始文件名
        String originalName = file.getOriginalFilename();
        if (!StringUtils.hasText(originalName)) throw new RuntimeException("文件上传失败");
        String suffix = originalName.substring(originalName.lastIndexOf("."));
        String fileName = prefix + suffix;
        //文件存储到我们设定的路径中
        file.transferTo(new File(path,fileName));
        //返回访问地址
        String fullUrl = baseUrl + prefix+suffix;
        System.out.println("图片上传成功");
        return ResponseObject.ok(fullUrl);
    }

    @PutMapping("/modify")
    ResponseObject modifyUser(@RequestBody UserVO userVO) {
        userService.modifyUser(userVO);
        return ResponseObject.ok();
    }

    @PostMapping("/modifyRoles/{roleIds}/{userId}")
    ResponseObject modifyRolesOfUser(@PathVariable String roleIds, @PathVariable String userId) {
        userService.modifyUserRole(roleIds,userId);
        return ResponseObject.ok();
    }
    @GetMapping("/id/{userId}")
    ResponseObject getUserById(@PathVariable String userId) {
        UserVO userById = userService.getUserByUserId(Long.parseLong(userId));
        return ResponseObject.ok(userById);
    }
}
